package cn.exrick.xboot.modules.Information.controller;

import cn.exrick.xboot.base.XbootBaseEntity;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.Information.entity.RiverUser;
import cn.exrick.xboot.modules.Information.entity.RiversSetUp;
import cn.exrick.xboot.modules.Information.service.IRiversSetUpService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author Lyg
 */
@Slf4j
@RestController
@Api(description = "巡河任务设置管理接口")
@RequestMapping("/xboot/riversSetUp")
@Transactional
public class RiversSetUpController {

    @Autowired
    private IRiversSetUpService iRiversSetUpService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<RiversSetUp> get(@PathVariable Long id){

        RiversSetUp riversSetUp = iRiversSetUpService.getById(id);
        return new ResultUtil<RiversSetUp>().setData(riversSetUp);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<RiversSetUp>> getAll(){

        List<RiversSetUp> list = iRiversSetUpService.list();
        return new ResultUtil<List<RiversSetUp>>().setData(list);
    }



    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Map>> getByPage(@ModelAttribute PageVo page,@RequestParam Map map){
        IPage<Map> mapIPage = iRiversSetUpService.selectAll(map,page);
//        IPage<RiversSetUp> data RiversSetUpService.aaaa(PageUtil.initMpPage(page),map);
        return new ResultUtil<IPage<Map>>().setData(mapIPage);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<RiversSetUp> saveOrUpdate(@RequestParam String riversSetUp){
        Gson gson = new Gson();
        List<RiversSetUp> riverssetup = gson.fromJson(riversSetUp, new TypeToken<List<RiversSetUp>>() {}.getType());
        try {
            riverssetup.forEach(rivers->{
                iRiversSetUpService.saveOrUpdate(rivers);
            });
            return new ResultUtil<RiversSetUp>().setData(new RiversSetUp());
        }catch (Exception e){
            e.printStackTrace();
            return new ResultUtil<RiversSetUp>().setErrorMsg("操作失败");
        }
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable Long[] ids){

        for(Long id : ids){
            iRiversSetUpService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

}
